<template>
  <div class="uploadImg">
    <el-upload
      class="avatar-uploader"
      action="javascript:;"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <div class="img-wrap">
        <div
          v-if="value"
          @mousemove="btnShow = true"
          @mouseleave="btnShow = false"
        >
          <img :src="value" class="avatar">
          <div v-show="btnShow && !percentageShow" class="btn-wrap">
            <i class="el-icon-zoom-in" @click.stop.prevent="previewHandle" />
            <i class="el-icon-delete" @click.stop.prevent="delHandle" />
          </div>
        </div>
        <i
          v-if="!value && !percentageShow"
          class="el-icon-plus avatar-uploader-icon"
        />
        <div v-show="percentageShow" class="percentage">
          <el-progress :width="160" type="circle" :percentage="percentage" />
        </div>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: String,
      default: ''
    },
    httpRequest: {
      type: Function,
      default: function(res) {
        // this.percentageShow = true
        this.$message.warning('httpRequest 是必传参数')
      }
    }
  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      btnShow: false,
      percentageShow: false,
      percentage: 0
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
        return false
      }
      return true
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    previewHandle() {
      // this.btnShow = false
      this.dialogVisible = true
      this.dialogImageUrl = this.value
    },
    delHandle() {
      this.$emit('input', '')
      this.btnShow = false
    }
  }
}
</script>
<styleb lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.img-wrap {
  position: relative;
  width: 178px;
  height: 178px;
  .btn-wrap {
    width: 178px;
    height: 178px;
    position: absolute;
    top: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.6);
    i {
      font-size: 18px;
      color: #fff;
      margin: 0 10px;
    }
  }
}
.percentage {
  position: absolute;
  top: 0;
  background: rgba(0, 0, 0, 0.6);
  .el-progress {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 178px;
    height: 178px;
    .el-progress__text {
      color: #fff;
    }
  }
}
</styleb>
